﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArchMethodLibrary.Utils;
using YJKDragCurveJig;

namespace YJKAxisAndEdit
{
	public class ArcAxisCreator
	{
		public ArcAxisCreator(ExternalCommandData cmdData, UIApplication application, ArcAxisDataFromDlg data)
		{
			this.m_cmdData = cmdData;
			this.m_uiApplication = application;
			this.m_document = application.ActiveUIDocument.Document;
			this.m_appCreator = application.Application.Create;
			this.m_docCreator = application.ActiveUIDocument.Document.Create;
			this.m_dataFromDlg = data;
		}

		private GridType ChangeLeftOrRightGridType(GridType gt, bool left, bool right, Autodesk.Revit.DB.Document doc)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			GridType gridType = gt;
			try
			{
				subTransaction.Start();
				if (left && !right)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Start", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Start") as GridType);
						Parameter parameter = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter2 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter.Set(1);
						parameter2.Set(0);
					}
				}
				else if (!left && right)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_End", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_End") as GridType);
						Parameter parameter3 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter4 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter3.Set(0);
						parameter4.Set(1);
					}
				}
				else if (left && right)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Both", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Both") as GridType);
						Parameter parameter5 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter6 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter5.Set(1);
						parameter6.Set(1);
					}
				}
				else if (!left && !right)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_None", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_None") as GridType);
						Parameter parameter7 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter8 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter7.Set(0);
						parameter8.Set(0);
					}
				}
				subTransaction.Commit();
			}
			catch
			{
				subTransaction.RollBack();
			}
			return gridType;
		}

		public static Element FindType(Autodesk.Revit.DB.Document doc, Type targetType, string familyName, string typeName, BuiltInCategory catagory)
		{
			IList<Element> list = ElementFiltering.FindSpecialElements(doc, targetType, catagory);
			Element result = null;
			foreach (Element element in list)
			{
				if (element.Name.Equals(typeName) && element.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_NAME).AsString().Equals(typeName))
				{
					result = element;
					break;
				}
			}
			return result;
		}

		private GridType ChangeUpOrDownGridType(GridType gt, bool up, bool down, Autodesk.Revit.DB.Document doc)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			GridType gridType = gt;
			try
			{
				subTransaction.Start();
				if (up && !down)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Start", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Start") as GridType);
						Parameter parameter = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter2 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter.Set(1);
						parameter2.Set(0);
					}
				}
				else if (!up && down)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_End", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_End") as GridType);
						Parameter parameter3 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter4 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter3.Set(0);
						parameter4.Set(1);
					}
				}
				else if (up && down)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_Both", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_Both") as GridType);
						Parameter parameter5 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter6 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter5.Set(1);
						parameter6.Set(1);
					}
				}
				else if (!up && !down)
				{
					gridType = (ArcAxisCreator.FindType(doc, typeof(GridType), gt.Name, gt.Name + "_None", BuiltInCategory.OST_Grids) as GridType);
					if (gridType == null)
					{
						gridType = (gt.Duplicate(gt.Name + "_None") as GridType);
						Parameter parameter7 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
						Parameter parameter8 = gridType.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
						parameter7.Set(0);
						parameter8.Set(0);
					}
				}
				subTransaction.Commit();
			}
			catch
			{
				subTransaction.RollBack();
			}
			return gridType;
		}

		public Result CreateByNormal(ref string message, Autodesk.Revit.DB.Document doc)
		{
			XYZ ptBase = new XYZ(0.0, 0.0, 0.0);
			XYZ ptBase2 = new XYZ(0.0, 0.0, 0.0);
			Transaction transaction = new Transaction(this.m_document, "create");
			transaction.Start();
			CurveArray curveArray = this.CreateLineAxisCurves(ptBase);
			CurveArray curveArray2 = this.CreateArcAxisCurves(ptBase);
			transaction.Commit();
			List<JigEdgeInfo> list = new List<JigEdgeInfo>();
			foreach (object obj in curveArray)
			{
				Curve curve = (Curve)obj;
				JigEdgeInfo item = new JigEdgeInfo(curve);
				list.Add(item);
			}
			foreach (object obj2 in curveArray2)
			{
				Curve curve2 = (Curve)obj2;
				JigEdgeInfo item2 = new JigEdgeInfo(curve2);
				list.Add(item2);
			}
			if (!DragCurveJigInterface.GetInsertPosition(this.m_cmdData, list, false, ptBase, ref ptBase2))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Transaction transaction2 = new Transaction(this.m_document, "temp");
			transaction2.Start();
			Result result;
			try
			{
				ElementSet elementSet = this.CreateLineAxis(ptBase2);
				ElementSet elementSet2 = this.CreateArcAxis(ptBase2);
				if (elementSet != null)
				{
					ElementSetIterator elementSetIterator;
					if (this.m_dataFromDlg.XReverse)
					{
						elementSetIterator = elementSet.ReverseIterator();
					}
					else
					{
						elementSetIterator = elementSet.ForwardIterator();
					}
					string text = this.m_dataFromDlg.XStart;
					while (elementSetIterator.MoveNext())
					{
						object obj3 = elementSetIterator.Current;
						Grid grid = obj3 as Grid;
						GridType gridTypeByName = AssistFunc.GetGridTypeByName(this.m_document, this.m_dataFromDlg.GridType);
						GridType gridType = this.ChangeLeftOrRightGridType(gridTypeByName, this.m_dataFromDlg.Left, this.m_dataFromDlg.Right, doc);
						grid.ChangeTypeId(gridType.Id);
						grid.Name = text;
						text = AxisNameManager.GetNextLable(text);
						ElementTransformUtils.MoveElement(this.m_document, grid.Id, XYZ.Zero);
					}
				}
				if (elementSet2 != null)
				{
					ElementSetIterator elementSetIterator2;
					if (this.m_dataFromDlg.YReverse)
					{
						elementSetIterator2 = elementSet2.ReverseIterator();
					}
					else
					{
						elementSetIterator2 = elementSet2.ForwardIterator();
					}
					string text2 = this.m_dataFromDlg.YStart;
					while (elementSetIterator2.MoveNext())
					{
						object obj4 = elementSetIterator2.Current;
						MultiSegmentGrid multiSegmentGrid = obj4 as MultiSegmentGrid;
						if (multiSegmentGrid != null)
						{
							multiSegmentGrid.Name = text2;
							ElementTransformUtils.MoveElement(this.m_document, multiSegmentGrid.Id, XYZ.Zero);
						}
						Grid grid2 = elementSetIterator2.Current as Grid;
						if (grid2 != null)
						{
							grid2.Name = text2;
							ElementTransformUtils.MoveElement(this.m_document, grid2.Id, XYZ.Zero);
						}
						text2 = AxisNameManager.GetNextLable(text2);
					}
				}
				if (this.m_dataFromDlg.WithDim)
				{
					this.CreateLinearAxisDim(elementSet2, this.m_dataFromDlg.ExtendL);
				}
				transaction2.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction2.RollBack();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				transaction2.RollBack();
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private void ChangeGridType(ref GridType gt, bool up, bool down, bool left, bool right)
		{
		}

		public void ViewValidating(Autodesk.Revit.DB.Document doc)
		{
			View activeView = doc.ActiveView;
			if (activeView is ViewPlan)
			{
				ViewPlan viewPlan = activeView as ViewPlan;
				if (viewPlan.SketchPlane == null)
				{
					Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewPlan.Origin, viewPlan.RightDirection, viewPlan.UpDirection);
					SubTransaction subTransaction = new SubTransaction(doc);
					subTransaction.Start();
					SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(doc, plane);
					activeView.SketchPlane = sketchPlane;
					subTransaction.Commit();
					return;
				}
			}
			else if (activeView is ViewSection)
			{
				ViewSection viewSection = activeView as ViewSection;
				if (viewSection.SketchPlane == null)
				{
					Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewSection.Origin, viewSection.RightDirection, viewSection.UpDirection);
					SubTransaction subTransaction2 = new SubTransaction(doc);
					subTransaction2.Start();
					SketchPlane sketchPlane2 = RevitVersionFuncs.NewSketchPlane(doc, plane2);
					activeView.SketchPlane = sketchPlane2;
					subTransaction2.Commit();
				}
			}
		}

		public Result CreateByPublic(ref string message)
		{
			Transaction transaction = new Transaction(this.m_document, "temp");
			transaction.Start();
			Result result;
			try
			{
				SingleAxisFilter singleAxisFilter = new SingleAxisFilter();
				Reference reference = this.m_uiApplication.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, singleAxisFilter, "请拾取公用的轴线：");
				Grid grid = this.m_document.GetElement(reference) as Grid;
				string name = this.m_document.GetElement(grid.GetTypeId()).Name;
				if (grid == null)
				{
					TaskDialog.Show("error", "请拾取单段轴线对象，暂不支持复合轴线！");
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					Line line = grid.Curve as Line;
					if (line == null)
					{
						TaskDialog.Show("error", "拾取的轴线不是直线轴线！");
						transaction.RollBack();
						result = Autodesk.Revit.UI.Result.Failed;
					}
					else
					{
						XYZ xyz = line.GetEndPoint(1);
						XYZ xyz2 = line.GetEndPoint(0);
						XYZ xyz3 = xyz2 - xyz;
						FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_uiApplication.ActiveUIDocument.Document);
						IList<Element> list = filteredElementCollector.OfClass(typeof(Grid)).ToElements();
						FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_uiApplication.ActiveUIDocument.Document);
						IList<Element> list2 = filteredElementCollector2.OfClass(typeof(MultiSegmentGrid)).ToElements();
						List<ElementId> list3 = new List<ElementId>();
						foreach (Element element in list2)
						{
							MultiSegmentGrid multiSegmentGrid = (MultiSegmentGrid)element;
							list3.AddRange(multiSegmentGrid.GetGridIds());
						}
						IntersectionResultArray intersectionResultArray = null;
						List<XYZ> list4 = new List<XYZ>();
						List<Element> list5 = new List<Element>();
						foreach (Element element2 in list)
						{
							if (!element2.Equals(grid) && !list3.Contains(element2.Id))
							{
								Grid grid2 = element2 as Grid;
								Curve curve = grid2.Curve;
								line.Intersect(curve, out intersectionResultArray);
								if (intersectionResultArray != null && intersectionResultArray.Size > 0 && this.IsVerticalIntersect(line, curve, intersectionResultArray.get_Item(0).XYZPoint))
								{
									XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
									list4.Add(xyzpoint);
									list5.Add(element2);
								}
							}
						}
						foreach (Element element3 in list2)
						{
							if (!element3.Equals(grid))
							{
								MultiSegmentGrid grid3 = element3 as MultiSegmentGrid;
								List<Curve> locationCurves = AxisAssistFunc.GetLocationCurves(this.m_uiApplication, this.m_document, grid3);
								foreach (Curve curve2 in locationCurves)
								{
									line.Intersect(curve2, out intersectionResultArray);
									if (intersectionResultArray != null && intersectionResultArray.Size > 0 && this.IsVerticalIntersect(line, curve2, intersectionResultArray.get_Item(0).XYZPoint))
									{
										XYZ xyzpoint2 = intersectionResultArray.get_Item(0).XYZPoint;
										list4.Add(xyzpoint2);
										list5.Add(element3);
									}
								}
							}
						}
						AssistFunc.CompressArray(list4);
						ArcAxisCreator.CompressArray(list5);
						XYZ xyz4 = list4.Last<XYZ>() - list4.First<XYZ>();
						if (GeoUtil.IsOppositeDirection(xyz3, xyz4))
						{
							xyz4 = -xyz4;
						}
						GeoUtil.SortPointByDirect(this.m_uiApplication, list4, xyz4);
						if (list4.Count <= 0)
						{
							result = Autodesk.Revit.UI.Result.Failed;
						}
						else
						{
							if (list4.Count > 1)
							{
								xyz = list4[0];
								xyz2 = list4[list4.Count - 1];
							}
							List<double> list6 = new List<double>();
							for (int i = 0; i < list4.Count - 1; i++)
							{
								XYZ xyz5 = list4.ElementAt(i);
								XYZ xyz6 = list4.ElementAt(i + 1);
								double val = xyz5.DistanceTo(xyz6);
								list6.Add(AssistFunc.feetToMM(val));
							}
							this.ViewValidating(this.m_document);
							XYZ xyz7 = this.m_uiApplication.ActiveUIDocument.Selection.PickPoint("请点取一点以确定轴网生成的方位！");
							int pointQuadrant = GeoUtil.GetPointQuadrant(xyz, xyz2, xyz7);
							ElementSet elementSet = new ElementSet();
							ElementSet elementSet2 = new ElementSet();
							XYZ zero = XYZ.Zero;
							this.CreateArcAxisByQuadrant(ref list5, line, xyz7, pointQuadrant, list4, list6, xyz4, xyz3, this.m_dataFromDlg.WithDim, ref elementSet, ref elementSet2, ref zero);
							this.m_document.Regenerate();
							transaction.Commit();
							if (this.m_dataFromDlg.WithDim)
							{
								Transaction transaction2 = new Transaction(this.m_document, "dim");
								transaction2.Start();
								try
								{
									ElementSetIterator elementSetIterator = elementSet2.ForwardIterator();
									elementSetIterator.MoveNext();
									MultiSegmentGrid multiSegmentGrid2 = elementSetIterator.Current as MultiSegmentGrid;
									GridType elem = this.m_document.GetElement(multiSegmentGrid2.GetTypeId()) as GridType;
									int num = elem.GetParameter(BuiltInParameter.GRID_CENTER_SEGMENT_STYLE).AsInteger();
									if (num != 0)
									{
										elem.GetParameter(BuiltInParameter.GRID_CENTER_SEGMENT_STYLE).Set(0);
										this.m_document.Regenerate();
									}
									this.CreateLinearMulitGridDim(elementSet2, zero);
									if (num != 0)
									{
										elem.GetParameter(BuiltInParameter.GRID_CENTER_SEGMENT_STYLE).Set(num);
									}
									transaction2.Commit();
								}
								catch
								{
									transaction2.RollBack();
								}
							}
							result = Autodesk.Revit.UI.Result.Succeeded;
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.RollBack();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private bool IsVerticalIntersect(Line baseLine, Curve curve, XYZ ptIntersect)
		{
			if (curve.GetType() == typeof(Line))
			{
				Line line = curve as Line;
				if (Geometry.IsVertical(baseLine.Direction, line.Direction, 1E-09))
				{
					return true;
				}
			}
			else if (curve.GetType() == typeof(Arc))
			{
				Arc arc = curve as Arc;
				if (Geometry.IsParallel(baseLine.Direction, arc.Center - ptIntersect, 1E-09))
				{
					return true;
				}
			}
			return false;
		}

		public static int CompressArray(List<Element> arElements)
		{
			List<Element> list = new List<Element>();
			foreach (Element element in arElements)
			{
				bool flag = false;
				foreach (Element element2 in list)
				{
					if (element2.Name == element.Name)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					list.Add(element);
				}
			}
			arElements.Clear();
			foreach (Element item in list)
			{
				arElements.Add(item);
			}
			return arElements.Count;
		}

		private void CreateArcAxisByQuadrant(ref List<Element> arGridConnect, Line baseLine, XYZ ptPick, int quadrant, List<XYZ> pts, List<double> dists, XYZ dir, XYZ baseline, bool isDim, ref ElementSet lineAxis, ref ElementSet arcAxis, ref XYZ ptCenter)
		{
			double val = 0.0;
			List<XYZ> list = new List<XYZ>();
			list.AddRange(pts);
			List<double> list2 = new List<double>();
			list2.AddRange(dists);
			XYZ xyz = dir;
			switch (quadrant)
			{
			case 1:
				list.Reverse();
				list2.Reverse();
				xyz = -xyz;
				val = baseline.AngleOnPlaneTo(XYZ.BasisX, -XYZ.BasisZ) + Math.PI;
				break;
			case 2:
				list.Reverse();
				list2.Reverse();
				xyz = -xyz;
				val = baseline.AngleOnPlaneTo(XYZ.BasisX, -XYZ.BasisZ) + Math.PI - AssistFunc.AngleToRadian(this.GetAllAngleLen());
				break;
			case 3:
				val = baseline.AngleOnPlaneTo(XYZ.BasisX, -XYZ.BasisZ);
				break;
			case 4:
				val = baseline.AngleOnPlaneTo(XYZ.BasisX, -XYZ.BasisZ) - AssistFunc.AngleToRadian(this.GetAllAngleLen());
				break;
			}
			ptCenter = list.First<XYZ>() - xyz.Normalize() * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius);
			this.m_dataFromDlg.StartAngle = AssistFunc.RadianToAngle(val);
			this.m_dataFromDlg.DistData.Clear();
			for (int i = 0; i < list2.Count; i++)
			{
				AxisListItem item = new AxisListItem(1, list2.ElementAt(i));
				this.m_dataFromDlg.DistData.Add(item);
			}
			lineAxis = this.CreateLineAxis(ptCenter, baseLine, quadrant);
			arcAxis = this.CreateConnectArcAxis(ref arGridConnect, baseLine, ptPick, ptCenter, quadrant, isDim);
		}

		public CurveArray CreateLineAxisCurves(XYZ ptBase)
		{
			CurveArray curveArray = new CurveArray();
			double num = this.m_dataFromDlg.StartAngle;
			Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, AssistFunc.AngleToRadian(num), ptBase);
			XYZ xyz = transform.OfVector(XYZ.BasisX).Normalize();
			ArcAxisCreator.m_dExtend = this.m_dataFromDlg.ExtendL;
			XYZ xyz2 = XYZ.Zero;
			XYZ xyz3 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + ArcAxisCreator.m_dExtend + this.GetAllRadiusLen());
			XYZ xyz4 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius - ArcAxisCreator.m_dExtend);
			if (xyz3.DistanceTo(ptBase) < xyz4.DistanceTo(ptBase))
			{
				xyz2 = xyz3;
				xyz3 = xyz4;
				xyz4 = xyz2;
			}
			Line line = YJKLineEx.YJKGetBound(xyz3, xyz4);
			Grid elem = null;
			OverlapType overlapGrid = this.GetOverlapGrid(line, out elem);
			if (overlapGrid == OverlapType.eNotOverlap)
			{
				curveArray.Append(line);
			}
			else if (overlapGrid == OverlapType.e1Cover2)
			{
				this.m_uiApplication.ActiveUIDocument.Document.DeleteElement(elem);
				curveArray.Append(line);
			}
			for (int i = 0; i < this.m_dataFromDlg.AngleData.Count; i++)
			{
				AxisListItem axisListItem = this.m_dataFromDlg.AngleData.ElementAt(i);
				int num2 = 0;
				while (num2 < axisListItem.Number && axisListItem.Dist > 0.0)
				{
					if (this.m_dataFromDlg.Clockwise && !this.m_dataFromDlg.SameAxis)
					{
						num -= axisListItem.Dist;
					}
					else
					{
						num += axisListItem.Dist;
					}
					transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, AssistFunc.AngleToRadian(num), ptBase);
					xyz = transform.OfVector(XYZ.BasisX).Normalize();
					xyz3 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius - ArcAxisCreator.m_dExtend);
					xyz4 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + ArcAxisCreator.m_dExtend + this.GetAllRadiusLen());
					line = YJKLineEx.YJKGetBound(xyz4, xyz3);
					overlapGrid = this.GetOverlapGrid(line, out elem);
					if (overlapGrid == OverlapType.eNotOverlap)
					{
						curveArray.Append(line);
					}
					else if (overlapGrid == OverlapType.e1Cover2)
					{
						this.m_uiApplication.ActiveUIDocument.Document.DeleteElement(elem);
						curveArray.Append(line);
					}
					num2++;
				}
			}
			return curveArray;
		}

		public CurveArray CreateLineAxisCurvesWithoutExtend(XYZ ptBase)
		{
			CurveArray curveArray = new CurveArray();
			CurveArray result;
			try
			{
				double num = this.m_dataFromDlg.StartAngle;
				Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, AssistFunc.AngleToRadian(num), ptBase);
				XYZ xyz = transform.OfVector(XYZ.BasisX).Normalize();
				ArcAxisCreator.m_dExtend = 0.0;
				XYZ xyz2 = XYZ.Zero;
				XYZ xyz3 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + ArcAxisCreator.m_dExtend + this.GetAllRadiusLen());
				XYZ xyz4 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius - ArcAxisCreator.m_dExtend);
				if (xyz3.DistanceTo(ptBase) < xyz4.DistanceTo(ptBase))
				{
					xyz2 = xyz3;
					xyz3 = xyz4;
					xyz4 = xyz2;
				}
				Line line = YJKLineEx.YJKGetBound(xyz3, xyz4);
				Grid elem = null;
				OverlapType overlapGrid = this.GetOverlapGrid(line, out elem);
				if (overlapGrid == OverlapType.eNotOverlap)
				{
					curveArray.Append(line);
				}
				else if (overlapGrid == OverlapType.e1Cover2)
				{
					this.m_uiApplication.ActiveUIDocument.Document.DeleteElement(elem);
					curveArray.Append(line);
				}
				for (int i = 0; i < this.m_dataFromDlg.AngleData.Count; i++)
				{
					AxisListItem axisListItem = this.m_dataFromDlg.AngleData.ElementAt(i);
					for (int j = 0; j < axisListItem.Number; j++)
					{
						if (this.m_dataFromDlg.Clockwise && !this.m_dataFromDlg.SameAxis)
						{
							num -= axisListItem.Dist;
						}
						else
						{
							num += axisListItem.Dist;
						}
						transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, AssistFunc.AngleToRadian(num), ptBase);
						xyz = transform.OfVector(XYZ.BasisX).Normalize();
						xyz3 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius - ArcAxisCreator.m_dExtend);
						xyz4 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + ArcAxisCreator.m_dExtend + this.GetAllRadiusLen());
						line = YJKLineEx.YJKGetBound(xyz4, xyz3);
						overlapGrid = this.GetOverlapGrid(line, out elem);
						if (overlapGrid == OverlapType.eNotOverlap)
						{
							curveArray.Append(line);
						}
						else if (overlapGrid == OverlapType.e1Cover2)
						{
							this.m_uiApplication.ActiveUIDocument.Document.DeleteElement(elem);
							curveArray.Append(line);
						}
					}
				}
				result = curveArray;
			}
			catch
			{
				result = curveArray;
			}
			return result;
		}

		private CurveArray CreateLineAxisCurvesWithOutExtend(XYZ ptBase, Line baseLine)
		{
			CurveArray curveArray = new CurveArray();
			double num = this.m_dataFromDlg.StartAngle;
			Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, AssistFunc.AngleToRadian(num), ptBase);
			XYZ xyz = transform.OfVector(XYZ.BasisX).Normalize();
			ArcAxisCreator.m_dExtend = this.m_dataFromDlg.ExtendL;
			XYZ xyz2 = XYZ.Zero;
			XYZ xyz3 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + this.GetAllRadiusLen());
			XYZ xyz4 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius);
			if (xyz3.DistanceTo(ptBase) < xyz4.DistanceTo(ptBase))
			{
				xyz2 = xyz3;
				xyz3 = xyz4;
				xyz4 = xyz2;
			}
			Line line = YJKLineEx.YJKGetBound(xyz3, xyz4);
			Grid elem = null;
			OverlapType overlapGrid = this.GetOverlapGrid(line, out elem);
			if (overlapGrid == OverlapType.eNotOverlap)
			{
				curveArray.Append(line);
			}
			else if (overlapGrid == OverlapType.e1Cover2)
			{
				this.m_uiApplication.ActiveUIDocument.Document.DeleteElement(elem);
				curveArray.Append(line);
			}
			for (int i = 0; i < this.m_dataFromDlg.AngleData.Count; i++)
			{
				AxisListItem axisListItem = this.m_dataFromDlg.AngleData.ElementAt(i);
				for (int j = 0; j < axisListItem.Number; j++)
				{
					if (this.m_dataFromDlg.Clockwise && !this.m_dataFromDlg.SameAxis)
					{
						num -= axisListItem.Dist;
					}
					else
					{
						num += axisListItem.Dist;
					}
					transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, AssistFunc.AngleToRadian(num), ptBase);
					xyz = transform.OfVector(XYZ.BasisX).Normalize();
					xyz3 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius);
					xyz4 = ptBase + xyz * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + this.GetAllRadiusLen());
					line = YJKLineEx.YJKGetBound(xyz4, xyz3);
					overlapGrid = this.GetOverlapGrid(line, out elem);
					if (overlapGrid == OverlapType.eNotOverlap)
					{
						curveArray.Append(line);
					}
					else if (overlapGrid == OverlapType.e1Cover2)
					{
						this.m_uiApplication.ActiveUIDocument.Document.DeleteElement(elem);
						curveArray.Append(line);
					}
				}
			}
			return curveArray;
		}

		private CurveArray CreateLineAxisCurves(XYZ ptBase, Line baseLine, int quadrant)
		{
			CurveArray curveArray = new CurveArray();
			XYZ xyz = baseLine.GetEndPoint(0);
			XYZ xyz2 = baseLine.GetEndPoint(1);
			XYZ xyz3 = xyz + (xyz2 - xyz) / 2.0;
			XYZ xyz4 = (xyz3 - ptBase).Normalize();
			double num = AssistFunc.RadianToAngle(XYZ.BasisX.AngleOnPlaneTo(xyz4, XYZ.BasisZ));
			ArcAxisCreator.m_dExtend = this.m_dataFromDlg.ExtendL;
			xyz = ptBase + xyz4 * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + ArcAxisCreator.m_dExtend + this.GetAllRadiusLen());
			xyz2 = ptBase + xyz4 * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius - ArcAxisCreator.m_dExtend);
			if (xyz.DistanceTo(ptBase) < xyz2.DistanceTo(ptBase))
			{
				XYZ xyz5 = xyz;
				xyz = xyz2;
				xyz2 = xyz5;
			}
			Line line = YJKLineEx.YJKGetBound(xyz, xyz2);
			Grid elem = null;
			OverlapType overlapGrid = this.GetOverlapGrid(line, out elem);
			if (overlapGrid == OverlapType.eNotOverlap)
			{
				curveArray.Append(line);
			}
			for (int i = 0; i < this.m_dataFromDlg.AngleData.Count; i++)
			{
				AxisListItem axisListItem = this.m_dataFromDlg.AngleData.ElementAt(i);
				for (int j = 0; j < axisListItem.Number; j++)
				{
					if (quadrant == 1 || quadrant == 3)
					{
						num += axisListItem.Dist;
					}
					else
					{
						num -= axisListItem.Dist;
					}
					Transform transform = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, AssistFunc.AngleToRadian(num), ptBase);
					xyz4 = transform.OfVector(XYZ.BasisX).Normalize();
					xyz = ptBase + xyz4 * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius - ArcAxisCreator.m_dExtend);
					xyz2 = ptBase + xyz4 * AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius + ArcAxisCreator.m_dExtend + this.GetAllRadiusLen());
					line = YJKLineEx.YJKGetBound(xyz2, xyz);
					overlapGrid = this.GetOverlapGrid(line, out elem);
					if (overlapGrid == OverlapType.eNotOverlap)
					{
						curveArray.Append(line);
					}
					else if (overlapGrid == OverlapType.e1Cover2)
					{
						this.m_uiApplication.ActiveUIDocument.Document.DeleteElement(elem);
						curveArray.Append(line);
					}
				}
			}
			return curveArray;
		}

		private ElementSet CreateLineAxis(XYZ ptBase)
		{
			string text = AxisNameManager.GetLargestNumber(this.m_document).ToString();
			CurveArray curves = this.CreateLineAxisCurves(ptBase);
			ElementSet newGrids = this.m_document.GetNewGrids(curves);
			foreach (object obj in newGrids)
			{
				Element element = (Element)obj;
				Grid grid = element as Grid;
				grid.Name = text + "-tmp";
				text = AxisNameManager.GetNextLable(text);
				grid.HyExtendToAllLevels();
			}
			return newGrids;
		}

		private ElementSet CreateLineAxis(XYZ ptBase, Line baseLine, int quadrant)
		{
			string text = AxisNameManager.GetLargestNumber(this.m_document).ToString();
			CurveArray curves = this.CreateLineAxisCurves(ptBase, baseLine, quadrant);
			ElementSet newGrids = this.m_document.GetNewGrids(curves);
			foreach (object obj in newGrids)
			{
				Element element = (Element)obj;
				Grid grid = element as Grid;
				grid.Name = text;
				text = AxisNameManager.GetNextLable(text);
				grid.HyExtendToAllLevels();
			}
			ElementSetIterator elementSetIterator = null;
			if (!this.m_dataFromDlg.XReverse)
			{
				elementSetIterator = newGrids.ReverseIterator();
			}
			else
			{
				elementSetIterator = newGrids.ForwardIterator();
			}
			string text2 = this.m_dataFromDlg.XStart;
			while (elementSetIterator.MoveNext())
			{
				object obj2 = elementSetIterator.Current;
				Grid grid2 = obj2 as Grid;
				GridType gridTypeByName = AssistFunc.GetGridTypeByName(this.m_document, this.m_dataFromDlg.GridType);
				grid2.ChangeTypeId(gridTypeByName.Id);
				try
				{
					grid2.Name = text2;
				}
				catch
				{
				}
				text2 = AxisNameManager.GetNextLable(text2);
			}
			return newGrids;
		}

		public CurveArray GetDimCurve(CurveArray gridCurve)
		{
			CurveArray curveArray = new CurveArray();
			CurveArray result;
			try
			{
				double val = 600.0;
				double val2 = 400.0;
				XYZ xyz = null;
				XYZ xyz2 = null;
				XYZ xyz3 = null;
				XYZ xyz4 = null;
				XYZ startPoint = null;
				XYZ endPoint = null;
				XYZ startPoint2 = null;
				XYZ endPoint2 = null;
				ReferenceArray referenceArray = new ReferenceArray();
				ReferenceArray referenceArray2 = new ReferenceArray();
				int num = 0;
				foreach (object obj in gridCurve)
				{
					Curve curve = (Curve)obj;
					Arc arc = curve as Arc;
					if (null != arc)
					{
						XYZ hyendPoint = arc.GetEndPoint(0);
						XYZ hyendPoint2 = arc.GetEndPoint(1);
						XYZ arcTangentVector = ArcAssistFunc.GetArcTangentVector(arc, hyendPoint);
						XYZ xyz5 = Geometry.RotateTo(arcTangentVector, -45.0, XYZ.BasisZ);
						XYZ arcTangentVector2 = ArcAssistFunc.GetArcTangentVector(arc, hyendPoint2);
						XYZ xyz6 = Geometry.RotateTo(arcTangentVector2, -45.0, XYZ.BasisZ);
						if (num == 0)
						{
							xyz = hyendPoint + arcTangentVector * AssistFunc.mmToFeet(val);
							xyz3 = hyendPoint2 - arcTangentVector2 * AssistFunc.mmToFeet(val);
							startPoint = hyendPoint - arcTangentVector * AssistFunc.mmToFeet(val2);
							startPoint2 = hyendPoint2 + arcTangentVector2 * AssistFunc.mmToFeet(val2);
							Line line = YJKLineEx.YJKGetBound(xyz - arcTangentVector * AssistFunc.mmToFeet(300.0), xyz + arcTangentVector * AssistFunc.mmToFeet(300.0));
							Line line2 = YJKLineEx.YJKGetBound(xyz - xyz5 * AssistFunc.mmToFeet(300.0), xyz + xyz5 * AssistFunc.mmToFeet(300.0));
							Line line3 = YJKLineEx.YJKGetBound(xyz3 - arcTangentVector2 * AssistFunc.mmToFeet(300.0), xyz3 + arcTangentVector2 * AssistFunc.mmToFeet(300.0));
							Line line4 = YJKLineEx.YJKGetBound(xyz3 - xyz6 * AssistFunc.mmToFeet(300.0), xyz3 + xyz6 * AssistFunc.mmToFeet(300.0));
							curveArray.Append(line);
							curveArray.Append(line2);
							curveArray.Append(line3);
							curveArray.Append(line4);
							referenceArray2.Append(curve.Reference);
						}
						else if (gridCurve.Size - 1 == num)
						{
							xyz2 = hyendPoint + arcTangentVector * AssistFunc.mmToFeet(val);
							xyz4 = hyendPoint2 - arcTangentVector2 * AssistFunc.mmToFeet(val);
							endPoint = hyendPoint - arcTangentVector * AssistFunc.mmToFeet(val2);
							endPoint2 = hyendPoint2 + arcTangentVector2 * AssistFunc.mmToFeet(val2);
							Line line5 = YJKLineEx.YJKGetBound(xyz2 - arcTangentVector * AssistFunc.mmToFeet(300.0), xyz2 + arcTangentVector * AssistFunc.mmToFeet(300.0));
							Line line6 = YJKLineEx.YJKGetBound(xyz2 - xyz5 * AssistFunc.mmToFeet(300.0), xyz2 + xyz5 * AssistFunc.mmToFeet(300.0));
							Line line7 = YJKLineEx.YJKGetBound(xyz4 - arcTangentVector2 * AssistFunc.mmToFeet(300.0), xyz4 + arcTangentVector2 * AssistFunc.mmToFeet(300.0));
							Line line8 = YJKLineEx.YJKGetBound(xyz4 - xyz6 * AssistFunc.mmToFeet(300.0), xyz4 + xyz6 * AssistFunc.mmToFeet(300.0));
							curveArray.Append(line5);
							curveArray.Append(line6);
							curveArray.Append(line7);
							curveArray.Append(line8);
							referenceArray2.Append(curve.Reference);
						}
						else
						{
							referenceArray.Append(curve.Reference);
						}
						num++;
					}
				}
				if (referenceArray.Size > 0)
				{
					Line line9 = YJKLineEx.YJKGetBound(xyz, xyz2);
					Line line10 = YJKLineEx.YJKGetBound(xyz3, xyz4);
					curveArray.Append(line9);
					curveArray.Append(line10);
				}
				if (referenceArray2.Size > 0)
				{
					YJKLineEx.YJKGetBound(startPoint, endPoint);
					YJKLineEx.YJKGetBound(startPoint2, endPoint2);
				}
				result = curveArray;
			}
			catch
			{
				result = curveArray;
			}
			return result;
		}

		public CurveArray GetArcDimCurve(CurveArray gridCurve)
		{
			CurveArray curveArray = new CurveArray();
			CurveArray result;
			try
			{
				double val = 600.0;
                Arc arc = gridCurve.get_Item(0) as Arc;
				XYZ xyz = arc.Center + (arc.GetEndPoint(0) - arc.Center).Normalize() * (arc.Radius - AssistFunc.mmToFeet(val));
				XYZ xyz2 = arc.Center + (arc.GetEndPoint(1) - arc.Center).Normalize() * (arc.Radius - AssistFunc.mmToFeet(val));
				XYZ radianPnt = arc.Center + (arc.Tessellate()[10] - arc.Center).Normalize() * (arc.Radius - AssistFunc.mmToFeet(val));
				Arc arc2 = this.m_document.CreatYJKArc(xyz, xyz2, radianPnt);
				Arc arc3 = gridCurve.get_Item(gridCurve.Size - 1) as Arc;
				XYZ xyz3 = arc3.Center + (arc3.GetEndPoint(0) - arc3.Center).Normalize() * (AssistFunc.mmToFeet(val) + arc3.Radius);
				XYZ xyz4 = arc3.Center + (arc3.GetEndPoint(1) - arc3.Center).Normalize() * (AssistFunc.mmToFeet(val) + arc3.Radius);
				XYZ radianPnt2 = arc3.Center + (arc3.Tessellate()[10] - arc3.Center).Normalize() * (AssistFunc.mmToFeet(val) + arc3.Radius);
				Arc arc4 = this.m_document.CreatYJKArc(xyz3, xyz4, radianPnt2);
				XYZ xyz5 = (xyz - arc2.Center).Normalize();
				XYZ xyz6 = Geometry.RotateTo(xyz5, -45.0, XYZ.BasisZ);
				XYZ xyz7 = (xyz2 - arc2.Center).Normalize();
				XYZ xyz8 = Geometry.RotateTo(xyz7, -45.0, XYZ.BasisZ);
				XYZ xyz9 = (xyz3 - arc4.Center).Normalize();
				XYZ xyz10 = Geometry.RotateTo(xyz9, -45.0, XYZ.BasisZ);
				XYZ xyz11 = (xyz3 - arc4.Center).Normalize();
				XYZ xyz12 = Geometry.RotateTo(xyz11, -45.0, XYZ.BasisZ);
				Line line = YJKLineEx.YJKGetBound(xyz - xyz5 * AssistFunc.mmToFeet(300.0), xyz + xyz5 * AssistFunc.mmToFeet(300.0));
				Line line2 = YJKLineEx.YJKGetBound(xyz - xyz6 * AssistFunc.mmToFeet(300.0), xyz + xyz6 * AssistFunc.mmToFeet(300.0));
				Line line3 = YJKLineEx.YJKGetBound(xyz2 - xyz7 * AssistFunc.mmToFeet(300.0), xyz2 + xyz7 * AssistFunc.mmToFeet(300.0));
				Line line4 = YJKLineEx.YJKGetBound(xyz2 - xyz8 * AssistFunc.mmToFeet(300.0), xyz2 + xyz8 * AssistFunc.mmToFeet(300.0));
				curveArray.Append(line);
				curveArray.Append(line2);
				curveArray.Append(line3);
				curveArray.Append(line4);
				Line line5 = YJKLineEx.YJKGetBound(xyz3 - xyz9 * AssistFunc.mmToFeet(300.0), xyz3 + xyz9 * AssistFunc.mmToFeet(300.0));
				Line line6 = YJKLineEx.YJKGetBound(xyz3 - xyz10 * AssistFunc.mmToFeet(300.0), xyz3 + xyz10 * AssistFunc.mmToFeet(300.0));
				Line line7 = YJKLineEx.YJKGetBound(xyz4 - xyz11 * AssistFunc.mmToFeet(300.0), xyz4 + xyz11 * AssistFunc.mmToFeet(300.0));
				Line line8 = YJKLineEx.YJKGetBound(xyz4 - xyz12 * AssistFunc.mmToFeet(300.0), xyz4 + xyz12 * AssistFunc.mmToFeet(300.0));
				curveArray.Append(line5);
				curveArray.Append(line6);
				curveArray.Append(line7);
				curveArray.Append(line8);
				curveArray.Append(arc2);
				curveArray.Append(arc4);
				result = curveArray;
			}
			catch
			{
				result = curveArray;
			}
			return result;
		}

		public CurveArray CreateArcAxisCurves(XYZ ptBase)
		{
			CurveArray curveArray = new CurveArray();
			double num = AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius);
			double num2 = AssistFunc.AngleToRadian(this.m_dataFromDlg.StartAngle);
			double num3 = AssistFunc.AngleToRadian(this.m_dataFromDlg.StartAngle + this.GetAllAngleLen());
			if (this.m_dataFromDlg.Clockwise && !this.m_dataFromDlg.SameAxis)
			{
				num2 = AssistFunc.AngleToRadian(this.m_dataFromDlg.StartAngle - this.GetAllAngleLen());
				num3 = AssistFunc.AngleToRadian(this.m_dataFromDlg.StartAngle);
			}
			if (Math.Abs(num3 - num2) > 0.001)
			{
				Arc arc = this.m_document.CreatYJKArc(ptBase, num, num2, num3, XYZ.BasisX, XYZ.BasisY);
				if (null != arc)
				{
					curveArray.Append(arc);
				}
				for (int i = 0; i < this.m_dataFromDlg.DistData.Count; i++)
				{
					AxisListItem axisListItem = this.m_dataFromDlg.DistData.ElementAt(i);
					num += AssistFunc.mmToFeet(axisListItem.Dist) * (double)axisListItem.Number;
					arc = this.m_document.CreatYJKArc(ptBase, num, num2, num3, XYZ.BasisX, XYZ.BasisY);
					if (null != arc)
					{
						curveArray.Append(arc);
					}
				}
			}
			return curveArray;
		}

		private ElementSet CreateArcAxis(XYZ ptBase)
		{
			string text = AxisNameManager.GetLargestNumber(this.m_document).ToString();
			CurveArray curveArray = this.CreateArcAxisCurves(ptBase);
			CurveArray curveArray2 = new CurveArray();
			CurveArray curveArray3 = new CurveArray();
			double num = AssistFunc.mmToFeet(this.m_dataFromDlg.ExtendA);
			ElementSet elementSet = new ElementSet();
			GridType gridTypeByName = AssistFunc.GetGridTypeByName(this.m_document, this.m_dataFromDlg.GridType);
			if (num > 0.0)
			{
				foreach (object obj in curveArray)
				{
					Curve curve = (Curve)obj;
					Arc arc = curve as Arc;
					XYZ arcTangentVector = ArcAssistFunc.GetArcTangentVector(arc, arc.GetEndPoint(1));
					Line line = YJKLineEx.YJKGetBound(arc.GetEndPoint(1), arc.GetEndPoint(1) - arcTangentVector * num);
					curveArray2.Append(line);
					XYZ arcTangentVector2 = ArcAssistFunc.GetArcTangentVector(arc, arc.GetEndPoint(0));
					Line line2 = YJKLineEx.YJKGetBound(arc.GetEndPoint(0), arc.GetEndPoint(0) + arcTangentVector2 * num);
					curveArray3.Append(line2);
				}
				SketchPlane sketchPlane = this.m_uiApplication.ActiveUIDocument.ActiveView.SketchPlane;
				ElementId id = this.ChangeUpOrDownGridType(gridTypeByName, this.m_dataFromDlg.Up, this.m_dataFromDlg.Down, this.m_document).Id;
				for (int i = 0; i < curveArray.Size; i++)
				{
					List<Curve> list = new List<Curve>();
					if (curveArray2.Size > i)
					{
                        list.Add(curveArray2.get_Item(i));
					}
                    list.Add(curveArray.get_Item(i));
					if (curveArray3.Size > i)
					{
                        list.Add(curveArray3.get_Item(i));
					}
					CurveLoop conCurvesLoop = AxisAssistFunc.GetConCurvesLoop(this.m_uiApplication, list);
					ElementId id2 = RevitDocumentExtension.CreateMultiSegmentGrid(this.m_document, id, conCurvesLoop, sketchPlane.Id);
					Element element = RevitVersionFuncs.GetElement(this.m_document, id2);
					elementSet.Insert(element);
				}
			}
			else
			{
				elementSet = this.m_document.GetNewGrids(curveArray);
			}
			foreach (object obj2 in elementSet)
			{
				Element element2 = (Element)obj2;
				MultiSegmentGrid multiSegmentGrid = element2 as MultiSegmentGrid;
				if (multiSegmentGrid != null)
				{
					multiSegmentGrid.Name = text + "-tmp";
				}
				Grid grid = element2 as Grid;
				if (grid != null)
				{
					grid.Name = text + "-tmp";
					GridType gridType = this.ChangeUpOrDownGridType(gridTypeByName, this.m_dataFromDlg.Up, this.m_dataFromDlg.Down, this.m_document);
					grid.ChangeTypeId(gridType.Id);
					grid.HyExtendToAllLevels();
				}
				text = AxisNameManager.GetNextLable(Convert.ToInt32(text).ToString());
			}
			return elementSet;
		}

		private ElementSet CreateConnectArcAxis(ref List<Element> arGridConnect, Line baseLine, XYZ ptPick, XYZ ptBase, int quadrant, bool isDim)
		{
			double num = AssistFunc.mmToFeet(this.m_dataFromDlg.InnerRadius);
			double startAngle = AssistFunc.AngleToRadian(this.m_dataFromDlg.StartAngle);
			double endAngle = AssistFunc.AngleToRadian(this.m_dataFromDlg.StartAngle + this.GetAllAngleLen());
			SketchPlane sketchPlane = this.m_uiApplication.ActiveUIDocument.ActiveView.SketchPlane;
			double num2 = AssistFunc.mmToFeet(this.m_dataFromDlg.ExtendA);
			List<Curve> list = new List<Curve>();
			CurveArray curveArray = new CurveArray();
			SubTransaction subTransaction = new SubTransaction(this.m_document);
			subTransaction.Start();
			Arc arc = this.m_document.CreatYJKArc(ptBase, num, startAngle, endAngle, XYZ.BasisX, XYZ.BasisY);
			curveArray.Append(arc);
			for (int i = 0; i < this.m_dataFromDlg.DistData.Count; i++)
			{
				AxisListItem axisListItem = this.m_dataFromDlg.DistData.ElementAt(i);
				num += AssistFunc.mmToFeet(axisListItem.Dist) * (double)axisListItem.Number;
				arc = this.m_document.CreatYJKArc(ptBase, num, startAngle, endAngle, XYZ.BasisX, XYZ.BasisY);
				curveArray.Append(arc);
			}
			subTransaction.Commit();
			YJKLineEx.YJKGetBound(XYZ.Zero, new XYZ(100.0, 0.0, 0.0));
			bool flag = false;
			ElementSet elementSet = new ElementSet();
			foreach (Element element in arGridConnect)
			{
				CurveLoop curveLoop = new CurveLoop();
				List<Curve> list2 = new List<Curve>();
				ElementId typeId = null;
				Curve curve = null;
				string name = "";
				if (element is Grid)
				{
					flag = true;
					Grid grid = element as Grid;
					typeId = grid.GetTypeId();
					curve = this.GetSingleContinueGridCurve(grid, baseLine, ptPick);
					list2.Add(curve);
					name = grid.Name;
				}
				else if (element is MultiSegmentGrid)
				{
					MultiSegmentGrid multiSegmentGrid = element as MultiSegmentGrid;
					typeId = multiSegmentGrid.GetTypeId();
					foreach (ElementId elementId in multiSegmentGrid.GetGridIds())
					{
						Grid grid2 = this.m_document.GetElement(elementId) as Grid;
						IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
						baseLine.Intersect(grid2.Curve, out intersectionResultArray);
						if (intersectionResultArray != null && intersectionResultArray.Size > 0)
						{
							XYZ ptTest = this.MiddlePoint(grid2.Curve.GetEndPoint(0), grid2.Curve.GetEndPoint(1));
							int pointQuadrant = GeoUtil.GetPointQuadrant(baseLine.GetEndPoint(1), baseLine.GetEndPoint(0), ptTest);
							if (pointQuadrant != quadrant && pointQuadrant + quadrant != 5)
							{
								curve = this.GetSingleContinueGridCurve(grid2, baseLine, ptPick);
								list2.Add(this.GetReverseCurve(curve, this.m_uiApplication));
							}
						}
						else
						{
							list2.Add(grid2.Curve);
						}
					}
					name = multiSegmentGrid.Name;
				}
				foreach (object obj in curveArray)
				{
					Curve curve2 = (Curve)obj;
					list.Clear();
					try
					{
						if (flag)
						{
							if (GeoUtil.IsPointEqual(curve.GetEndPoint(0), curve2.GetEndPoint(0)) || GeoUtil.IsPointEqual(curve.GetEndPoint(0), curve2.GetEndPoint(1)) || GeoUtil.IsPointEqual(curve.GetEndPoint(1), curve2.GetEndPoint(0)) || GeoUtil.IsPointEqual(curve.GetEndPoint(1), curve2.GetEndPoint(1)))
							{
								if (num2 > 0.0)
								{
									Arc arc2 = curve2 as Arc;
									if (quadrant == 1 || quadrant == 3)
									{
										XYZ arcTangentVector = ArcAssistFunc.GetArcTangentVector(arc2, arc2.GetEndPoint(1));
										Line item = YJKLineEx.YJKGetBound(arc2.GetEndPoint(1), arc2.GetEndPoint(1) - arcTangentVector * num2);
										list.Add(item);
									}
									else if (quadrant == 2 || quadrant == 4)
									{
										XYZ arcTangentVector2 = ArcAssistFunc.GetArcTangentVector(arc2, arc2.GetEndPoint(0));
										Line item2 = YJKLineEx.YJKGetBound(arc2.GetEndPoint(0), arc2.GetEndPoint(0) + arcTangentVector2 * num2);
										list.Add(item2);
									}
								}
								list.Add(curve2);
								list.Add(curve);
								curveLoop = AxisAssistFunc.GetConCurvesLoop(this.m_uiApplication, list);
								break;
							}
						}
						else
						{
							if (GeoUtil.IsPointEqual(curve.GetEndPoint(0), curve2.GetEndPoint(0)))
							{
								break;
							}
							if (GeoUtil.IsPointEqual(curve.GetEndPoint(1), curve2.GetEndPoint(1)))
							{
								if (CurveAssistFunc.GetOverlapType(this.m_uiApplication, list2[0], curve) == OverlapType.eOverlap)
								{
									if (curve is Arc)
									{
										Arc arc3 = curve2 as Arc;
										XYZ arcTangentVector3 = ArcAssistFunc.GetArcTangentVector(arc3, arc3.GetEndPoint(0));
										if (num2 > 0.0)
										{
											Line line = YJKLineEx.YJKGetBound(arc3.GetEndPoint(0) + arcTangentVector3 * num2, arc3.GetEndPoint(0));
											curveLoop.Append(line);
										}
										curveLoop.Append(curve2);
										using (List<Curve>.Enumerator enumerator4 = list2.GetEnumerator())
										{
											while (enumerator4.MoveNext())
											{
												Curve curve3 = enumerator4.Current;
												if (curve3 is Arc)
												{
													curveLoop.Append(curve3);
												}
												else
												{
													curveLoop.Append(this.GetReverseCurve(curve3, this.m_uiApplication));
												}
											}
											break;
										}
									}
									Arc arc4 = curve2 as Arc;
									XYZ arcTangentVector4 = ArcAssistFunc.GetArcTangentVector(arc4, arc4.GetEndPoint(0));
									if (num2 > 0.0)
									{
										Line line2 = YJKLineEx.YJKGetBound(arc4.GetEndPoint(0) + arcTangentVector4 * num2, arc4.GetEndPoint(0));
										curveLoop.Append(line2);
									}
									curveLoop.Append(curve2);
									using (List<Curve>.Enumerator enumerator4 = list2.GetEnumerator())
									{
										while (enumerator4.MoveNext())
										{
											Curve curve4 = enumerator4.Current;
											if (curve4 is Arc)
											{
												curveLoop.Append(curve4);
											}
											else
											{
												curveLoop.Append(this.GetReverseCurve(curve4, this.m_uiApplication));
											}
										}
										break;
									}
								}
								if (curve is Arc)
								{
									curveLoop = AxisAssistFunc.GetConCurvesLoop(this.m_uiApplication, list2);
									curveLoop.Append(this.GetReverseCurve(curve2, this.m_uiApplication));
									Arc arc5 = curve2 as Arc;
									XYZ arcTangentVector5 = ArcAssistFunc.GetArcTangentVector(arc5, arc5.GetEndPoint(0));
									if (num2 > 0.0)
									{
										Line line3 = YJKLineEx.YJKGetBound(arc5.GetEndPoint(0), arc5.GetEndPoint(0) + arcTangentVector5 * num2);
										curveLoop.Append(line3);
									}
								}
								else
								{
									foreach (Curve curve5 in list2)
									{
										if (curve5 is Arc)
										{
											curveLoop.Append(curve5);
										}
										else
										{
											curveLoop.Append(this.GetReverseCurve(curve5, this.m_uiApplication));
										}
									}
									curveLoop.Append(this.GetReverseCurve(curve2, this.m_uiApplication));
									Arc arc6 = curve2 as Arc;
									XYZ arcTangentVector6 = ArcAssistFunc.GetArcTangentVector(arc6, arc6.GetEndPoint(0));
									Line line4 = YJKLineEx.YJKGetBound(arc6.GetEndPoint(0), arc6.GetEndPoint(0) + arcTangentVector6 * num2);
									curveLoop.Append(line4);
								}
								break;
							}
							if (GeoUtil.IsPointEqual(curve.GetEndPoint(0), curve2.GetEndPoint(1)))
							{
								break;
							}
							if (GeoUtil.IsPointEqual(curve.GetEndPoint(1), curve2.GetEndPoint(0)))
							{
								if (CurveAssistFunc.GetOverlapType(this.m_uiApplication, list2[0], curve) == OverlapType.eOverlap)
								{
									bool flag2 = false;
									if (curve is Arc)
									{
										if (num2 > 0.0)
										{
											Arc arc7 = curve2 as Arc;
											XYZ arcTangentVector7 = ArcAssistFunc.GetArcTangentVector(arc7, arc7.GetEndPoint(1));
											Line line5 = YJKLineEx.YJKGetBound(arc7.GetEndPoint(1) - arcTangentVector7 * num2, arc7.GetEndPoint(1));
											curveLoop.Append(line5);
										}
										curveLoop.Append(this.GetReverseCurve(curve2, this.m_uiApplication));
										using (List<Curve>.Enumerator enumerator4 = list2.GetEnumerator())
										{
											while (enumerator4.MoveNext())
											{
												Curve curve6 = enumerator4.Current;
												if (curve6 is Arc)
												{
													if (flag2 && curve6.Equals(list2[list2.Count - 1]))
													{
														curveLoop.Append(this.GetReverseCurve(curve6, this.m_uiApplication));
													}
													else
													{
														curveLoop.Append(curve6);
													}
												}
												else
												{
													curveLoop.Append(this.GetReverseCurve(curve6, this.m_uiApplication));
													if (!curve6.Equals(list2[0]))
													{
														flag2 = true;
													}
												}
											}
											break;
										}
									}
									if (num2 > 0.0)
									{
										Arc arc8 = curve2 as Arc;
										XYZ arcTangentVector8 = ArcAssistFunc.GetArcTangentVector(arc8, arc8.GetEndPoint(1));
										Line line6 = YJKLineEx.YJKGetBound(arc8.GetEndPoint(1) - arcTangentVector8 * num2, arc8.GetEndPoint(1));
										curveLoop.Append(line6);
									}
									curveLoop.Append(this.GetReverseCurve(curve2, this.m_uiApplication));
									for (int j = list2.Count - 1; j >= 0; j--)
									{
										Curve curve7 = list2[j];
										if (curve7 is Arc)
										{
											curveLoop.Append(curve7);
										}
										else
										{
											curveLoop.Append(this.GetReverseCurve(curve7, this.m_uiApplication));
										}
									}
								}
								else if (curve is Arc)
								{
									curveLoop = AxisAssistFunc.GetConCurvesLoop(this.m_uiApplication, list2);
									curveLoop.Append(curve2);
									if (num2 > 0.0)
									{
										Arc arc9 = curve2 as Arc;
										XYZ arcTangentVector9 = ArcAssistFunc.GetArcTangentVector(arc9, arc9.GetEndPoint(1));
										Line line7 = YJKLineEx.YJKGetBound(arc9.GetEndPoint(1), arc9.GetEndPoint(1) - arcTangentVector9 * num2);
										curveLoop.Append(line7);
									}
								}
								else
								{
									foreach (Curve curve8 in list2)
									{
										if (curve8 is Arc)
										{
											curveLoop.Append(curve8);
										}
										else
										{
											curveLoop.Append(this.GetReverseCurve(curve8, this.m_uiApplication));
										}
									}
									curveLoop.Append(curve2);
									if (num2 > 0.0)
									{
										Arc arc10 = curve2 as Arc;
										XYZ arcTangentVector10 = ArcAssistFunc.GetArcTangentVector(arc10, arc10.GetEndPoint(1));
										Line line8 = YJKLineEx.YJKGetBound(arc10.GetEndPoint(1), arc10.GetEndPoint(1) - arcTangentVector10 * num2);
										curveLoop.Append(line8);
									}
								}
								break;
							}
						}
					}
					catch (Exception)
					{
						break;
					}
				}
				ElementId id = RevitDocumentExtension.CreateMultiSegmentGrid(this.m_document, typeId, curveLoop, sketchPlane.Id);
				Element element2 = RevitVersionFuncs.GetElement(this.m_document, id);
				MultiSegmentGrid multiSegmentGrid2 = element2 as MultiSegmentGrid;
				multiSegmentGrid2.Name = name;
				elementSet.Insert(element2);
				this.m_document.DeleteElement(element);
			}
			return elementSet;
		}

		private XYZ MiddlePoint(XYZ spt, XYZ ept)
		{
			return new XYZ((spt.X + ept.X) / 2.0, (spt.Y + ept.Y) / 2.0, (spt.Z + ept.Z) / 2.0);
		}

		private Curve GetReverseCurve(Curve cur, UIApplication uiApp)
		{
			Curve result;
			if (cur is Line)
			{
				XYZ hyendPoint = cur.GetEndPoint(0);
				XYZ hyendPoint2 = cur.GetEndPoint(1);
				result = YJKLineEx.YJKGetBound(hyendPoint2, hyendPoint);
			}
			else if (cur is Arc)
			{
				Arc arc = cur as Arc;
				XYZ hyendPoint3 = cur.GetEndPoint(0);
				XYZ hyendPoint4 = cur.GetEndPoint(1);
				XYZ radianPnt = arc.Tessellate()[1];
				XYZ center = arc.Center;
				result = this.m_document.CreatYJKArc(hyendPoint4, hyendPoint3, radianPnt);
			}
			else
			{
				result = cur;
			}
			return result;
		}

		private void GetSingleContinueGridCurves(ref CurveArray oriCurves, Grid g, Line baseLine)
		{
			IntersectionResultArray intersectionResultArray = null;
			Curve curve = g.Curve;
			baseLine.Intersect(curve, out intersectionResultArray);
			if (intersectionResultArray != null)
			{
				Line line = curve as Line;
				if (line != null)
				{
					Line line2;
					if (curve.GetEndPoint(0).DistanceTo(intersectionResultArray.get_Item(0).XYZPoint) < curve.GetEndPoint(1).DistanceTo(intersectionResultArray.get_Item(0).XYZPoint))
					{
						line2 = YJKLineEx.YJKGetBound(curve.GetEndPoint(1), intersectionResultArray.get_Item(0).XYZPoint);
					}
					else
					{
						line2 = YJKLineEx.YJKGetBound(curve.GetEndPoint(0), intersectionResultArray.get_Item(0).XYZPoint);
					}
					oriCurves.Append(line2);
				}
				Arc arc = curve as Arc;
				if (arc != null)
				{
					Arc arc2;
					if (curve.GetEndPoint(0).DistanceTo(intersectionResultArray.get_Item(0).XYZPoint) < curve.GetEndPoint(1).DistanceTo(intersectionResultArray.get_Item(0).XYZPoint))
					{
						arc2 = this.m_document.CreatYJKArc(curve.GetEndPoint(1), intersectionResultArray.get_Item(0).XYZPoint, ArcAssistFunc.GetArcMidPoint(arc));
					}
					else
					{
						arc2 = this.m_document.CreatYJKArc(curve.GetEndPoint(0), intersectionResultArray.get_Item(0).XYZPoint, ArcAssistFunc.GetArcMidPoint(arc));
					}
					oriCurves.Append(arc2);
				}
			}
		}

		private Curve GetSingleContinueGridCurve(Grid g, Line baseLine, XYZ ptPick)
		{
			Curve result = null;
			IntersectionResultArray intersectionResultArray = null;
			Curve curve = g.Curve;
			baseLine.Intersect(curve, out intersectionResultArray);
			if (intersectionResultArray != null && intersectionResultArray.Size > 0)
			{
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
				XYZ xyzpoint2 = curve.Project(ptPick).XYZPoint;
				Line line = curve as Line;
				if (line != null)
				{
					XYZ vec = (hyendPoint2 - hyendPoint).Normalize();
					XYZ vec2 = (xyzpoint2 - xyzpoint).Normalize();
					Line line2;
					if (vec.IsSameDirectionTo(vec2))
					{
						line2 = YJKLineEx.YJKGetBound(hyendPoint, xyzpoint);
					}
					else
					{
						line2 = YJKLineEx.YJKGetBound(hyendPoint2, xyzpoint);
					}
					result = line2;
				}
				Arc arc = curve as Arc;
				if (arc != null)
				{
					Arc arc2;
					if (hyendPoint.DistanceTo(xyzpoint) < hyendPoint2.DistanceTo(xyzpoint))
					{
						arc2 = this.m_document.CreatYJKArc(hyendPoint2, xyzpoint, ArcAssistFunc.GetArcMidPoint(arc));
					}
					else
					{
						arc2 = this.m_document.CreatYJKArc(hyendPoint, xyzpoint, ArcAssistFunc.GetArcMidPoint(arc));
					}
					result = arc2;
				}
			}
			return result;
		}

		private void CreateLinearAxisDim(ElementSet arAxis, double exLength)
		{
			double val = exLength - 1500.0;
			double val2 = exLength - 800.0;
			XYZ startPoint = null;
			XYZ endPoint = null;
			XYZ startPoint2 = null;
			XYZ endPoint2 = null;
			XYZ startPoint3 = null;
			XYZ endPoint3 = null;
			XYZ startPoint4 = null;
			XYZ endPoint4 = null;
			ReferenceArray referenceArray = new ReferenceArray();
			ReferenceArray referenceArray2 = new ReferenceArray();
			List<Grid> list = new List<Grid>();
			foreach (object obj in arAxis)
			{
				Element element = (Element)obj;
				MultiSegmentGrid multiSegmentGrid = element as MultiSegmentGrid;
				if (multiSegmentGrid != null)
				{
					foreach (ElementId elementId in multiSegmentGrid.GetGridIds())
					{
						list.Add(this.m_document.GetElement(elementId) as Grid);
					}
				}
				Grid grid = element as Grid;
				if (grid != null)
				{
					list.Add(grid);
				}
			}
			if (list.Count < 2)
			{
				return;
			}
			int num = 0;
			double num2 = 10000000000.0;
			double num3 = 0.0;
			foreach (Grid grid2 in list)
			{
				Curve curve = grid2.Curve;
				Arc arc = curve as Arc;
				if (null != arc)
				{
					if (arc.Radius > num3)
					{
						num3 = arc.Radius;
					}
					if (arc.Radius < num2)
					{
						num2 = arc.Radius;
					}
					XYZ hyendPoint = arc.GetEndPoint(0);
					XYZ hyendPoint2 = arc.GetEndPoint(1);
					XYZ xyz = ArcAssistFunc.GetArcTangentVector(arc, hyendPoint).Normalize();
					XYZ xyz2 = ArcAssistFunc.GetArcTangentVector(arc, hyendPoint2).Normalize();
					Reference reference = null;
					this.GetGridRef(grid2, ref reference);
					if (num == 0)
					{
						startPoint = hyendPoint - xyz * AssistFunc.mmToFeet(val);
						startPoint2 = hyendPoint2 + xyz2 * AssistFunc.mmToFeet(val);
						startPoint3 = hyendPoint - xyz * AssistFunc.mmToFeet(val2);
						startPoint4 = hyendPoint2 + xyz2 * AssistFunc.mmToFeet(val2);
						referenceArray2.Append(reference);
						referenceArray.Append(reference);
					}
					else if (arAxis.Size - 1 == num)
					{
						endPoint = hyendPoint - xyz * AssistFunc.mmToFeet(val);
						endPoint2 = hyendPoint2 + xyz2 * AssistFunc.mmToFeet(val);
						endPoint3 = hyendPoint - xyz * AssistFunc.mmToFeet(val2);
						endPoint4 = hyendPoint2 + xyz2 * AssistFunc.mmToFeet(val2);
						referenceArray2.Append(reference);
						referenceArray.Append(reference);
					}
					else
					{
						referenceArray2.Append(reference);
					}
					num++;
				}
			}
			DimensionType lineDimType = this.m_dataFromDlg.LineDimType;
			DimensionType arcDimType = this.m_dataFromDlg.ArcDimType;
			if (lineDimType == null || arcDimType == null)
			{
				TaskDialog.Show("Error", "没有找到标注样式！");
				return;
			}
			if (referenceArray.Size > 0 && referenceArray2.Size > 2)
			{
				Line line = YJKLineEx.YJKGetBound(startPoint, endPoint);
				Line line2 = YJKLineEx.YJKGetBound(startPoint2, endPoint2);
				this.m_docCreator.NewDimension(this.m_document.ActiveView, line, referenceArray, lineDimType);
				this.m_docCreator.NewDimension(this.m_document.ActiveView, line2, referenceArray, lineDimType);
			}
			if (referenceArray2.Size > 0)
			{
				Line line3 = YJKLineEx.YJKGetBound(startPoint3, endPoint3);
				Line line4 = YJKLineEx.YJKGetBound(startPoint4, endPoint4);
				this.m_docCreator.NewDimension(this.m_document.ActiveView, line3, referenceArray2, lineDimType);
				this.m_docCreator.NewDimension(this.m_document.ActiveView, line4, referenceArray2, lineDimType);
			}
		}

		public DimensionType GetDimTypeByName(Autodesk.Revit.DB.Document doc, string name, DimensionStyleType styleType)
		{
			DimensionType result = null;
			List<DimensionType> dimensionTypes = Common.GetDimensionTypes(doc);
			foreach (DimensionType dimensionType in dimensionTypes)
			{
				if (dimensionType.Name.ToString().Equals(name) && dimensionType.StyleType.Equals(styleType))
				{
					result = dimensionType;
				}
			}
			return result;
		}

		private void CreateLinearMulitGridDim(ElementSet arAxis, XYZ center)
		{
			DimensionType lineDimType = this.m_dataFromDlg.LineDimType;
			if (lineDimType == null)
			{
				TaskDialog.Show("Error", "没有找到标注样式！");
				return;
			}
			List<ArcAxisCreator.DimData> list = new List<ArcAxisCreator.DimData>();
			List<ArcAxisCreator.DimData> list2 = new List<ArcAxisCreator.DimData>();
			foreach (object obj in arAxis)
			{
				Element element = (Element)obj;
				MultiSegmentGrid multiSegmentGrid = element as MultiSegmentGrid;
				if (multiSegmentGrid != null)
				{
					this.GetMutilGridRef(multiSegmentGrid, ref list, ref list2);
				}
			}
			ReferenceArray referenceArray = new ReferenceArray();
			foreach (ArcAxisCreator.DimData dimData in list)
			{
				referenceArray.Append(dimData.Ref);
			}
			XYZ startPoint = XYZ.Zero;
			XYZ endPoint = XYZ.Zero;
			if (center.DistanceTo(list[0].FirstDimPoint) < center.DistanceTo(list[list.Count - 1].FirstDimPoint))
			{
				startPoint = list[0].FirstDimPoint;
				endPoint = list[list.Count - 1].FirstDimPoint;
			}
			else
			{
				endPoint = list[0].FirstDimPoint;
				startPoint = list[list.Count - 1].FirstDimPoint;
			}
			Line line = YJKLineEx.YJKGetBound(startPoint, endPoint);
			this.m_docCreator.NewDimension(this.m_document.ActiveView, line, referenceArray, lineDimType);
			ReferenceArray referenceArray2 = new ReferenceArray();
			referenceArray2.Append(list[0].Ref);
			referenceArray2.Append(list[list.Count - 1].Ref);
			XYZ startPoint2 = XYZ.Zero;
			XYZ endPoint2 = XYZ.Zero;
			if (center.DistanceTo(list[0].SecendDimPoint) < center.DistanceTo(list[list.Count - 1].SecendDimPoint))
			{
				startPoint2 = list[0].SecendDimPoint;
				endPoint2 = list[list.Count - 1].SecendDimPoint;
			}
			else
			{
				endPoint2 = list[0].SecendDimPoint;
				startPoint2 = list[list.Count - 1].SecendDimPoint;
			}
			Line line2 = YJKLineEx.YJKGetBound(startPoint2, endPoint2);
			this.m_docCreator.NewDimension(this.m_document.ActiveView, line2, referenceArray2, lineDimType);
			ReferenceArray referenceArray3 = new ReferenceArray();
			foreach (ArcAxisCreator.DimData dimData2 in list2)
			{
				referenceArray3.Append(dimData2.Ref);
			}
			XYZ startPoint3 = XYZ.Zero;
			XYZ endPoint3 = XYZ.Zero;
			if (center.DistanceTo(list[0].FirstDimPoint) > center.DistanceTo(list[list.Count - 1].FirstDimPoint))
			{
				startPoint3 = list2[0].FirstDimPoint;
				endPoint3 = list2[list.Count - 1].FirstDimPoint;
			}
			else
			{
				endPoint3 = list2[0].FirstDimPoint;
				startPoint3 = list2[list.Count - 1].FirstDimPoint;
			}
			Line line3 = YJKLineEx.YJKGetBound(startPoint3, endPoint3);
			this.m_docCreator.NewDimension(this.m_document.ActiveView, line3, referenceArray3, lineDimType);
			ReferenceArray referenceArray4 = new ReferenceArray();
			referenceArray4.Append(list2[0].Ref);
			referenceArray4.Append(list2[list.Count - 1].Ref);
			XYZ startPoint4 = XYZ.Zero;
			XYZ endPoint4 = XYZ.Zero;
			if (center.DistanceTo(list[0].SecendDimPoint) > center.DistanceTo(list[list.Count - 1].SecendDimPoint))
			{
				startPoint4 = list2[0].SecendDimPoint;
				endPoint4 = list2[list.Count - 1].SecendDimPoint;
			}
			else
			{
				endPoint4 = list2[0].SecendDimPoint;
				startPoint4 = list2[list.Count - 1].SecendDimPoint;
			}
			Line line4 = YJKLineEx.YJKGetBound(startPoint4, endPoint4);
			this.m_docCreator.NewDimension(this.m_document.ActiveView, line4, referenceArray4, lineDimType);
		}

		private void GetMutilGridRef(MultiSegmentGrid mgrid, ref List<ArcAxisCreator.DimData> lstFirstGrid, ref List<ArcAxisCreator.DimData> lstLastGrid)
		{
			List<Grid> list = new List<Grid>();
			foreach (ElementId elementId in mgrid.GetGridIds())
			{
				Grid grid = this.m_document.GetElement(elementId) as Grid;
				if (grid.Curve.Reference != null)
				{
					list.Add(grid);
				}
			}
			Reference reference = null;
			double num = AssistFunc.mmToFeet(1300.0);
			double num2 = AssistFunc.mmToFeet(600.0);
			if (list.Count == 1)
			{
				Grid grid2 = list[0];
				Curve curve = grid2.Curve;
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				this.GetGridRef(grid2, ref reference);
				if (curve is Arc)
				{
					XYZ arcTangentVector = ArcAssistFunc.GetArcTangentVector(curve as Arc, hyendPoint);
					XYZ secendDimPoint = hyendPoint - arcTangentVector * num;
					XYZ firstDimPoint = hyendPoint - arcTangentVector * num2;
					lstFirstGrid.Add(new ArcAxisCreator.DimData(firstDimPoint, secendDimPoint, reference));
					arcTangentVector = ArcAssistFunc.GetArcTangentVector(curve as Arc, hyendPoint2);
					secendDimPoint = hyendPoint2 - arcTangentVector * num;
					firstDimPoint = hyendPoint2 - arcTangentVector * num2;
					lstLastGrid.Add(new ArcAxisCreator.DimData(firstDimPoint, secendDimPoint, reference));
					return;
				}
				if (curve is Line)
				{
					XYZ xyz = (hyendPoint2 - hyendPoint).Normalize();
					XYZ xyz2 = hyendPoint + xyz * num2;
					XYZ xyz3 = hyendPoint + xyz * num;
					lstFirstGrid.Add(new ArcAxisCreator.DimData(xyz3, xyz2, reference));
					xyz2 = hyendPoint2 - xyz * num;
					xyz3 = hyendPoint2 - xyz * num2;
					lstLastGrid.Add(new ArcAxisCreator.DimData(xyz2, xyz3, reference));
					return;
				}
			}
			else if (list.Count > 1)
			{
				Grid grid3 = list[0];
				Curve curve2 = grid3.Curve;
				XYZ hyendPoint3 = curve2.GetEndPoint(0);
				XYZ hyendPoint4 = curve2.GetEndPoint(1);
				Grid grid4 = list[1];
				Curve curve3 = grid4.Curve;
				XYZ hyendPoint5 = curve3.GetEndPoint(0);
				XYZ hyendPoint6 = curve3.GetEndPoint(1);
				XYZ xyz4 = hyendPoint3;
				if (hyendPoint3.DistanceTo(hyendPoint5) < 0.01 || hyendPoint3.DistanceTo(hyendPoint6) < 0.01)
				{
					xyz4 = hyendPoint4;
				}
				this.GetGridRef(grid3, ref reference);
				if (curve2 is Arc)
				{
					XYZ arcTangentVector2 = ArcAssistFunc.GetArcTangentVector(curve2 as Arc, xyz4);
					XYZ secendDimPoint2 = xyz4 - arcTangentVector2 * num;
					XYZ firstDimPoint2 = xyz4 - arcTangentVector2 * num2;
					lstFirstGrid.Add(new ArcAxisCreator.DimData(firstDimPoint2, secendDimPoint2, reference));
				}
				else if (curve2 is Line)
				{
					XYZ xyz5 = (hyendPoint3 - hyendPoint4).Normalize();
					if (xyz4.DistanceTo(hyendPoint4) < 0.01)
					{
						xyz5 = (hyendPoint4 - hyendPoint3).Normalize();
					}
					XYZ firstDimPoint3 = xyz4 - xyz5 * num;
					XYZ secendDimPoint3 = xyz4 - xyz5 * num2;
					lstFirstGrid.Add(new ArcAxisCreator.DimData(firstDimPoint3, secendDimPoint3, reference));
				}
				Grid grid5 = list[list.Count - 1];
				Curve curve4 = grid5.Curve;
				hyendPoint3 = curve4.GetEndPoint(0);
				hyendPoint4 = curve4.GetEndPoint(1);
				Grid grid6 = list[list.Count - 2];
				Curve curve5 = grid6.Curve;
				XYZ hyendPoint7 = curve5.GetEndPoint(0);
				XYZ hyendPoint8 = curve5.GetEndPoint(1);
				XYZ xyz6 = hyendPoint3;
				if (hyendPoint3.DistanceTo(hyendPoint7) < 0.01 || hyendPoint3.DistanceTo(hyendPoint8) < 0.01)
				{
				}
				this.GetGridRef(grid5, ref reference);
				if (curve4 is Arc)
				{
					XYZ arcTangentVector3 = ArcAssistFunc.GetArcTangentVector(curve4 as Arc, xyz6);
					XYZ secendDimPoint4 = xyz6 - arcTangentVector3 * num;
					XYZ firstDimPoint4 = xyz6 - arcTangentVector3 * num2;
					lstLastGrid.Add(new ArcAxisCreator.DimData(firstDimPoint4, secendDimPoint4, reference));
					return;
				}
				if (curve4 is Line)
				{
					XYZ xyz7 = (hyendPoint3 - hyendPoint4).Normalize();
					if (xyz6.DistanceTo(hyendPoint4) < 0.01)
					{
						xyz7 = (hyendPoint4 - hyendPoint3).Normalize();
					}
					XYZ firstDimPoint5 = xyz6 - xyz7 * num;
					XYZ secendDimPoint5 = xyz6 - xyz7 * num2;
					lstLastGrid.Add(new ArcAxisCreator.DimData(firstDimPoint5, secendDimPoint5, reference));
				}
			}
		}

		private bool GetGridRef(Grid grid, ref Reference reference)
		{
			XYZ hyendPoint = grid.Curve.GetEndPoint(0);
			XYZ hyendPoint2 = grid.Curve.GetEndPoint(1);
			List<Curve> elementLines = this.GetElementLines(grid);
			foreach (Curve curve in elementLines)
			{
				if (curve.Reference != null)
				{
					XYZ hyendPoint3 = curve.GetEndPoint(0);
					XYZ hyendPoint4 = curve.GetEndPoint(1);
					XYZ xyz = null;
					XYZ xyz2 = null;
					Geometry.OverlapType overlapType = Geometry.IsOverlapOfTwoLine(this.To2D(hyendPoint), this.To2D(hyendPoint2), this.To2D(hyendPoint3), this.To2D(hyendPoint4), ref xyz, ref xyz2, true, 1E-09);
					if (overlapType == Geometry.OverlapType._OT_Equality)
					{
						reference = curve.Reference;
						return true;
					}
				}
			}
			reference = grid.Curve.Reference;
			return true;
		}

		private XYZ To2D(XYZ pt)
		{
			return new XYZ(pt.X, pt.Y, 0.0);
		}

		private List<Curve> GetElementLines(Element e)
		{
			List<Curve> list = new List<Curve>();
			GeometryElement ge = e.get_Geometry(new Options
			{
				ComputeReferences = true,
				IncludeNonVisibleObjects = true,
				View = this.m_document.ActiveView
			});
			List<GeometryObject> geometryObjectArray = RevitVersionFuncs.GetGeometryObjectArray(ge);
			for (int i = 0; i < geometryObjectArray.Count; i++)
			{
				GeometryObject geometryObject = geometryObjectArray[i];
				if (geometryObject is Line)
				{
					Line item = geometryObject as Line;
					list.Add(item);
				}
				else if (geometryObject is Arc)
				{
					Arc item2 = geometryObject as Arc;
					list.Add(item2);
				}
			}
			return list;
		}

		private double GetAllRadiusLen()
		{
			double num = 0.0;
			for (int i = 0; i < this.m_dataFromDlg.DistData.Count; i++)
			{
				AxisListItem axisListItem = this.m_dataFromDlg.DistData.ElementAt(i);
				num += axisListItem.Dist * (double)axisListItem.Number;
			}
			return num;
		}

		private double GetAllAngleLen()
		{
			double num = 0.0;
			for (int i = 0; i < this.m_dataFromDlg.AngleData.Count; i++)
			{
				AxisListItem axisListItem = this.m_dataFromDlg.AngleData.ElementAt(i);
				num += axisListItem.Dist * (double)axisListItem.Number;
			}
			return num;
		}

		private OverlapType GetOverlapGrid(Line newline, out Grid overlapGrid)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_uiApplication.ActiveUIDocument.Document);
			IList<Element> list = filteredElementCollector.OfClass(typeof(Grid)).ToElements();
			overlapGrid = null;
			OverlapType overlapType = OverlapType.eNotOverlap;
			foreach (Element element in list)
			{
				Grid grid = element as Grid;
				Line line = grid.Curve as Line;
				if (!(line == null))
				{
					overlapType = LineAssistFunc.GetOverlapType(this.m_uiApplication, newline, line);
					if (overlapType != OverlapType.eNotOverlap)
					{
						overlapGrid = grid;
						return overlapType;
					}
				}
			}
			return overlapType;
		}

		private ExternalCommandData m_cmdData;

		private UIApplication m_uiApplication;

        private Autodesk.Revit.DB.Document m_document;

		private Application m_appCreator;

        private Autodesk.Revit.Creation.Document m_docCreator;

		private ArcAxisDataFromDlg m_dataFromDlg;

		private static double m_dExtend = 3000.0;

		private class DimData
		{
			public XYZ FirstDimPoint { get; set; }

			public XYZ SecendDimPoint { get; set; }

			public Reference Ref { get; set; }

			public DimData(XYZ firstDimPoint, XYZ secendDimPoint, Reference reference)
			{
				this.FirstDimPoint = firstDimPoint;
				this.SecendDimPoint = secendDimPoint;
				this.Ref = reference;
			}
		}
	}
}
